package vashion.azeroth.core.common;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by tingting on 15/9/5.
 * 运营操作校验器，目前所有后台操作走这一个公共校验器
 */
@Aspect
public class OperatePermissionAspect {

    private final static Logger log = LoggerFactory.getLogger(OperatePermissionAspect.class);

    @Before(value = "@annotation(vashion.azeroth.core.common.NeedPermission)")
    public void hasOperatePermission(JoinPoint joinPoint) {
        try {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            NeedPermission needPermission = signature.getMethod().getAnnotation(NeedPermission.class);
            long operateId = getInnerId(joinPoint, needPermission.operateIdArgLocation());
            //FIXME 校验id

        } catch (Exception e) {
            log.error("OperatePermissionAspect.hasOperatePermission error", e);
        }
    }

    private long getInnerId(JoinPoint joinPoint, int operateIdLocation) {
        Object[] args = joinPoint.getArgs();
        if (null == args || args.length == 0 || args.length <= operateIdLocation) {
            return 0l;
        }
        Object operateIdObject = args[operateIdLocation];
        if (operateIdObject instanceof Long) {
            return (Long) operateIdObject;
        }
        return 0;
    }

}
